package com.fifth.apartment.controller;

import cn.hutool.core.bean.BeanUtil;
import com.fifth.apartment.config.LoginUser;
import com.fifth.apartment.entity.*;
import com.fifth.apartment.result.Result;
import com.fifth.apartment.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.jam.mutable.MElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.fifth.apartment.constant.MessageConstant;


@RestController
@RequestMapping("/admin/menu")
@Api(tags = "菜单管理接口")
@Slf4j
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("查询所有菜单")
    @GetMapping("MenuList")
    private Result MenuList(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser principal = (LoginUser) authentication.getPrincipal();
        User user = principal.getUser();
        Set<Menu> menuSet = new HashSet<>();
        user.getRoles().stream().forEach(role -> {
            role.getMenus().forEach(menu -> {
                menuSet.add(menu);
            });
        });
        List<Menu> list = menuSet.stream().collect(Collectors.toList());
        List<MVo> menus = menuService.selectList(list);

        if (!menus.isEmpty()){
            return new Result(true,MessageConstant.QUERY_SELECT_MENU_LIST_TRUE,menus );
        }
        return new Result(true,MessageConstant.QUERY_SELECT_MENU_LIST_FAIL, menus);
    }
}
